package com.oazon.sonos.util;

import android.content.Context;

import com.oazon.moco.bean.AuthType;
import com.oazon.moco.bean.DeviceLinkAuthParams;
import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.sonos.core.AbsServiceHelper;

public class DeviceLinkHelper extends AbsServiceHelper {
	
	protected static McServiceMsg serviceMsg;
	
	public static McServiceMsg getServiceMsg(){
		return serviceMsg;
	}
	static {
		serviceMsg = new McServiceMsg<DeviceLinkAuthParams>();
	
		serviceMsg.setUri("http://music.163.com/ws/smapi-1.0/Sonos");
		serviceMsg.setName("网易云音乐");
		serviceMsg.setAuthType(AuthType.DEVICE_LINK.value());
		serviceMsg.setSecureUri("https://music.163.com/ws/smapi-1.0/Sonos");
		
		DeviceLinkAuthParams authParams = new DeviceLinkAuthParams();
		authParams.setDeviceProvider("Sonos");
		authParams.setHouseholdid("Sonos_6YOXBpVpgIV6yZZPS9Gtx9701Z");
		authParams.setKey("6a5f8160d1c6cdc550bf7fef7c6a77291cc63e5996da249fb1");
		authParams.setToken("72be0a486a66df4d126a3c21dafdbc59728a59b674963b0312");	
		serviceMsg.setAuthParams(authParams);
	}
	public DeviceLinkHelper(Context context,McServiceMsg serviceMsg) {
		this.context = context;
		this.url = serviceMsg.getUri();
		registMcServiceMsg(serviceMsg);
/*		this.sHeader = "<s:Header>"
				+ "<credentials xmlns=\"http://www.sonos.com/Services/1.1\">"
				+ "<loginToken>"
				+ "<token>72be0a486a66df4d126a3c21dafdbc59728a59b674963b0312</token>"
				+ "<key>6a5f8160d1c6cdc550bf7fef7c6a77291cc63e5996da249fb1</key>"
				+ "<householdId>Sonos_6YOXBpVpgIV6yZZPS9Gtx9701Z</householdId>"
				+ "</loginToken>" + "<deviceId>" + LinkSDK.UUID + "</deviceId>"
				+ "<deviceProvider>Sonos</deviceProvider>" + "</credentials>"
				+ "</s:Header>";*/
	}

	public void registMcServiceMsg(McServiceMsg msg) {
		if (msg == null || !(msg.getAuthParams() instanceof DeviceLinkAuthParams)) {
			return;
		}
		DeviceLinkAuthParams params = (DeviceLinkAuthParams) msg
				.getAuthParams();

		String token = params.getToken();
		if (token == null) {
			token = "72be0a486a66df4d126a3c21dafdbc59728a59b674963b0312";
		}

		String key = params.getKey();
		if (key == null) {
			key = "6a5f8160d1c6cdc550bf7fef7c6a77291cc63e5996da249fb1";
		}

		String houseHoldId = params.getHouseholdid();
		if (houseHoldId == null) {
			houseHoldId = "Sonos_6YOXBpVpgIV6yZZPS9Gtx9701Z";
		}

		String deviceProvider = params.getDeviceProvider();
		if (deviceProvider == null) {
			deviceProvider = "Sonos";
		}

		this.sHeader = "<s:Header>"
				+ "<credentials xmlns=\"http://www.sonos.com/Services/1.1\">"
				+ "<loginToken>" //
				+ "<token>" + token + "</token>" //
				+ "<key>" + key + "</key>" //
				+ "<householdId>" + houseHoldId + "</householdId>"//
				+ "</loginToken>" //
				+ "<deviceId>" + LinkSDK.UUID + "</deviceId>" //
				+ "<deviceProvider>" + deviceProvider + "</deviceProvider>"//
				+ "</credentials>" + "</s:Header>";
	}

	@Override
	public McServiceMsg getMcServiceMsg() {
		return serviceMsg;
	}

}
